<template>
    <div class="mod-prod">
        <avue-crud ref="crud"
                   :search="searchQuery"
                   :data="dataList"
                   :option="tableOption"
                   :page="page"
                   :permission="permission"
                   :table-loading="dataListLoading"
                   @search-change="searchChange"
                   @selection-change="selectionChange"
                   @row-save="sourceRowSave"
                   @row-del="rowDel"
                   @refresh-change="getDataList(page)"
        >

            <template slot="menuLeft">
                <el-button type="primary"
                           icon="el-icon-plus"
                           size="small"
                           @click.stop="addOrUpdateHandle()">新增</el-button>

<!--                <el-button type="danger"-->
<!--                           @click="deleteHandle()"-->
<!--                           size="small"-->
<!--                           :disabled="dataListSelections.length <= 0">批量删除</el-button>-->
            </template>
            <template slot="status"
                      slot-scope="scope">
                <el-tag v-if="scope.row.status === 1"
                        size="small">上架
                </el-tag>
                <el-tag v-else
                        size="small">未上架
                </el-tag>
            </template>

            <template slot="menu"
                      slot-scope="scope">

            </template>
        </avue-crud>
        <!-- 弹窗, 新增 / 修改 -->
        <add-or-update v-if="addOrUpdateVisible"
                       ref="addOrUpdate"
                       @refreshDataList="getDataList(page,searchQuery)"></add-or-update>
    </div>
</template>

<script>
import {tableOption} from '@/crud/relationship/next'
import AddOrUpdate from './next-add-or-update.vue'
export default {
	components: {
		AddOrUpdate
	},
	data () {
		return {
			addOrUpdateVisible: false,
			dataForm: {
                busiOppoId: ''
			},
            searchQuery: {
                busiOppoId: ''
            },
			permission: {
				delBtn: this.isAuth('prod:prod:delete')
			},
			dataList: [],
			page: {
				total: 0, // 总页数
				currentPage: 1, // 当前页数
				pageSize: 10 // 每页显示多少条
			},
			dataListSelections: [],
			dataListLoading: false,
			tableOption: tableOption,
			resourcesUrl: process.env.VUE_APP_RESOURCES_URL
		}
	},
    mounted () {
        this.init()
    },
    methods: {
        init(){
            this.$http({
                url: this.$http.adornUrl('/crm/oppo/page'),
                method: 'get',
                params: this.$http.adornParams(
                    Object.assign(
                        {
                            current: 1,
                            size: 100
                        },
                    )
                )
            }).then(({data}) => {
                console.log('🚀 ~ data', data)
                this.tableOption.column.forEach((col) => {
                    if(col.prop === "busiOppoId"){
                        col.dicData = []
                        data.obj.records.forEach((d) => {
                            col.dicData.push({
                                value: d.id,
                                label: d.buyIntention
                            })
                        })
                        this.searchQuery.busiOppoId = col.dicData[0].value;
                        this.getDataList(this.page,this.searchQuery)
                    }
                })
            })
        },
		// 获取数据列表
		getDataList (page, params, done) {
			
            console.log('🚀 ~ params', params)
            if(!params || !params.busiOppoId){
                return this.$message.warning("请选择一个产品意向")
            }
            this.dataListLoading = true
            this.dataForm.busiOppoId = params && params.busiOppoId ? params.busiOppoId : null
			this.$http({
				url: this.$http.adornUrl('/crm/oppo/trace/page'),
				method: 'get',
				params: this.$http.adornParams(
					Object.assign(
						{
							current: page == null ? this.page.currentPage : page.currentPage,
							size: page == null ? this.page.pageSize : page.pageSize
						},
						params
					)
				)
			}).then(({data}) => {
				this.dataList = data.obj.records
				this.page.total = data.obj.total
				this.dataListLoading = false
				if (done) {
					done()
				}
			})
		},
		// 新增 / 修改
		addOrUpdateHandle (id) {
            this.addOrUpdateVisible = true
            this.$nextTick(() => {
                this.$refs.addOrUpdate.init(id)
            })
		},
		// 删除和批量删除
		deleteHandle (id) {
			let prodIds = this.getSeleProdIds()
			if (id) {
				prodIds.push(id)
			}
			this.$confirm(`确定进行[${id ? '删除' : '批量删除'}]操作?`, '友情提醒', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			})
				.then(() => {
					this.$http({
						url: this.$http.adornUrl(`/crm/oppo/trace`),
						method: 'delete',
						data: this.$http.adornData(prodIds, false)
					}).then(({data}) => {
						this.$message({
							message: '操作成功',
							type: 'success',
							duration: 1500,
							onClose: () => {
								this.getDataList(this.page)
							}
						})
					})
				})
				.catch(() => {
				})
		},
		rowDel: function (row, index) {
			this.$confirm('确定进行删除操作?', '友情提醒', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				this.$http({
					url: this.$http.adornUrl('/crm/oppo/trace/' + row.id),
					method: 'delete',
					data: this.$http.adornData({})
				}).then(({data}) => {
					this.$message({
						message: '操作成功',
						type: 'success',
						duration: 1500,
						onClose: () => {
							this.getDataList()
						}
					})
				})
			}).catch(() => {
			})
		},
		sourceRowSave (form, done, loading) {
			console.log('🚀 ~ form', form)
			this.$http({
				url: this.$http.adornUrl('/crm/base/prod/saveProd'),
				method: 'post',
				data: this.$http.adornData({
					prodId: wfy.uuid(20),
					prodCode: form.prodCode,
					prodName: form.prodName,
					prodSpec: form.prodSpec,
					prodLenwidth: form.prodLenwidth,
				})
			}).then(({data}) => {
				this.$message({
					message: '操作成功',
					type: 'success',
					duration: 1500,
					onClose: () => {
						if (done) {
							done()
						}
						this.getDataList(this.page)
					}
				})
			})
		},
		// 条件查询
		searchChange (params, done) {
			this.getDataList(this.page, params, done)
		},
		// 多选变化
		selectionChange (val) {
			this.dataListSelections = val
		},
		// 获取选中的商品Id列表
		getSeleProdIds () {
			return this.dataListSelections.map(item => {
				return item.prodId
			})
		}
	}
}
</script>
